{% extends "base.html" %}
{% block content %}
<h1>Got a pb with given arguments?</h1>
<p>You might want to interrupt view process while building up your context...</p>
<p> 
  Let's say you want to anticipate your jetlag and calculate what time
  it is over the world. Basically, you want to shift local time by a
  value between [-12,-12]. This view will raise a
  HttpResponseBadRequest if you give something else as
  <code>shift</code></p>
<p>So here it is: time where I will go (soon): <strong>{{ datetime }}</strong></p>
<p>
  Give a try with working shifts, <a href="/time/-2/">-2</a>, <a
  href="/time/4/">4</a>, <a href="/time/8/">8</a>; and non working
  ones: <a href="/time/-50/">-50</a>, <a href="/time/16/">16</a></p>
<div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">obviews.views</span> <span class="kn">import</span> <span class="n">View</span><span class="p">,</span> <span class="n">FormView</span><span class="p">,</span> <span class="n">CutResponse</span>

<span class="c"># stop process at any point</span>
<span class="k">class</span> <span class="nc">ProcessView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;time_shift.html&quot;</span>

    <span class="k">def</span> <span class="nf">_some_function</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shift</span><span class="p">):</span>
        <span class="n">shift</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">shift</span><span class="p">)</span> <span class="c"># integer guaranteed by regexp in URL</span>
        <span class="k">if</span> <span class="n">shift</span> <span class="o">&lt;</span> <span class="o">-</span><span class="mi">12</span> <span class="ow">or</span> <span class="n">shift</span> <span class="o">&gt;</span> <span class="mi">12</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">CutResponse</span><span class="p">(</span><span class="n">HttpResponseBadRequest</span><span class="p">(</span>
                <span class="s">&quot;shift must be in [-12,12]&quot;</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">+</span><span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="n">shift</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">shift</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="c"># calling other function for the sake of the demo, exception</span>
        <span class="c"># can be raised anywhere</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;datetime&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_some_function</span><span class="p">(</span><span class="n">shift</span><span class="p">)}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>
    <span class="p">(</span><span class="s">&#39;^time/(?P&lt;shift&gt;-?\d+)/$&#39;</span><span class="p">,</span> <span class="n">ProcessView</span><span class="p">()),</span>
<span class="p">)</span>

</pre></div>
<p>
  Finally, the <strong>last example</strong> deals with
  testing... or printing... or exporting... any case when you do want
  <a href="/about/raw/">raw HTML</a>, meaning not wrapped into some
  response object.
</p>
{% endblock %}
